package tables

import (
	"github.com/GoAdminGroup/go-admin/context"
	"github.com/GoAdminGroup/go-admin/modules/db"
	"github.com/GoAdminGroup/go-admin/plugins/admin/modules/table"
	"github.com/GoAdminGroup/go-admin/template/types"
	"github.com/GoAdminGroup/go-admin/template/types/form"
	editType "github.com/GoAdminGroup/go-admin/template/types/table"
)

func GetPaymentRecordTable(ctx *context.Context) table.Table {

	paymentRecord := table.NewDefaultTable(table.DefaultConfigWithDriver("mysql"))

	info := paymentRecord.GetInfo()
	info.SetFilterFormLayout(form.LayoutTwoCol)
	info.AddField("Id", "id", db.Int).
		FieldFilterable().
		FieldSortable()
	info.AddField("订单", "name", db.Varchar).
		FieldJoin(types.Join{
			Table:     "orders",
			JoinField: "id",
			Field:     "order_id",
		}).FieldFilterable(types.FilterType{FormType: form.SelectSingle}).
		FieldFilterOptionsFromTable("orders", "name", "name", func(model *db.SQL) *db.SQL {
			return model.Where("id", ">", 0)
		}).
		FieldSortable()
	info.AddField("收款类型", "payment_type", db.Tinyint).
		FieldDisplay(func(model types.FieldModel) interface{} {
			switch model.Value {
			case "1": // 启用
				return "定金"
			case "2": // 弃用
				return "尾款"
			case "3": // 弃用
				return "箱子押金"
			case "4": // 弃用
				return "箱子回款"
			default: // 未知
				return "未知"
			}
		}).
		FieldFilterable(types.FilterType{FormType: form.SelectSingle}).
		FieldFilterOptions(types.FieldOptions{
			{Text: "定金", Value: "1"},
			{Text: "尾款", Value: "2"},
			{Text: "箱子押金", Value: "3"},
			{Text: "箱子回款", Value: "4"},
			{Text: "未知", Value: "0"},
		}).
		FieldEditAble(editType.Select).FieldEditOptions(types.FieldOptions{
		{Text: "定金", Value: "1"},
		{Text: "尾款", Value: "2"},
		{Text: "箱子押金", Value: "3"},
		{Text: "箱子回款", Value: "4"},
		{Text: "未知", Value: "0"},
	}).
		FieldSortable()
	info.AddField("收款数量", "amount", db.Decimal).
		FieldFilterable().
		FieldSortable().
		FieldEditAble()
	info.AddField("播种管理员", "username", db.Varchar).
		FieldJoin(types.Join{
			Table:     "goadmin_users",
			JoinField: "id",
			Field:     "operator_id",
		}).FieldFilterable(types.FilterType{FormType: form.SelectSingle}).
		FieldFilterOptionsFromTable("goadmin_users", "username", "username", func(model *db.SQL) *db.SQL {
			return model.Where("id", ">", 2)
		}).
		FieldSortable()
	info.AddField("备注", "remark", db.Varchar).
		FieldFilterable().
		FieldSortable().
		FieldEditAble()
	info.AddField("创建时间", "created_at", db.Timestamp).
		FieldSortable()
	info.AddField("更新时间", "updated_at", db.Timestamp).
		FieldSortable()

	info.SetTable("payment_record").SetTitle("收款记录").SetDescription("PaymentRecord")

	formList := paymentRecord.GetForm()
	formList.AddField("Id", "id", db.Int, form.Default).
		FieldDisableWhenCreate().
		FieldDisableWhenUpdate()
	formList.AddField("订单", "order_id", db.Int, form.SelectSingle).FieldOptionsFromTable("orders", "name", "id", func(model *db.SQL) *db.SQL {
		return model.Where("id", ">", 0)
	}).FieldHelpMsg("请选择订单")
	formList.AddField("收款类型", "payment_type", db.Tinyint, form.Radio).
		// radio的选项，Text代表显示内容，Value代表对应值
		FieldOptions(types.FieldOptions{
			{Text: "定金", Value: "1"},
			{Text: "尾款", Value: "2"},
			{Text: "箱子押金", Value: "3"},
			{Text: "箱子回款", Value: "4"},
			{Text: "未知", Value: "0"},
		}).FieldDefault("0")
	formList.AddField("金额", "amount", db.Decimal, form.Text)
	formList.AddField("操作员", "operator_id", db.Int, form.SelectSingle).FieldOptionsFromTable("goadmin_users", "username", "id", func(model *db.SQL) *db.SQL {
		return model.Where("id", ">", 2)
	}).FieldHelpMsg("请选择操作员")
	formList.AddField("备注", "remark", db.Varchar, form.Text)
	formList.AddField("创建时间", "created_at", db.Timestamp, form.Datetime).
		FieldDisableWhenUpdate().
		FieldHide().FieldNowWhenInsert()
	formList.AddField("修改时间", "updated_at", db.Timestamp, form.Datetime).
		FieldDisableWhenCreate().
		FieldHide().FieldNowWhenUpdate()

	formList.SetTable("payment_record").SetTitle("收款记录").SetDescription("PaymentRecord")

	return paymentRecord
}
